var app = new Vue({
    el: '.box',
    data: {
        nav: [],
        item: [],
        item_data: true,
        leng: '',
        open: false,
        num: 16,
        page: 0,
        refreshing: false,
        loading: false,
        text: 'List',
        loaded_all: false,
        panel: '',
        tag: '',
        new_items: '',
        hot_items: '',
        expands: true,
        links: '',
        msg_data: '',
        ad_data: '',
        like_arr: [],
        openAlert: false,
        timer: 5000
    },
    methods: {
        refresh() {
            this.refreshing = true;
            this.$refs.container.scrollTop = 500;
            setTimeout(() => {
                location.reload()
            }, 2000)
        }, load() {
            if (this.item_data) {
                this.loading = true;
                app.page++;
                var num = app.page * 10;
                get_info(101, 0, num);
                setTimeout(() => {
                    app.loading = false
                }, 2000)
            }
        }, toggle(panel) {
            this.panel = panel === this.panel ? '' : panel
        }, set_tips(obj, flag) {
            if (flag) {
                return '更新时间: ' + obj
            } else {
                return '已有 ' + obj + ' 人看过'
            }
        }, like_click(index, id) {
            $('.like')[index].classList.add('like_clicks');
            this.like_arr = localStorage.getItem('like') ? JSON.parse(localStorage.getItem('like')) : [];
            var len = this.like_arr.length || 0;
            for (var i = 0; i < len; i++) {
                if (id == this.like_arr[i]) {
                    return false
                }
            };
            this.like_arr.push(id);
            var arr = Array.from(new Set(this.like_arr));
            localStorage.setItem('like', JSON.stringify(arr));
            get_info(109, id)
        }, gift_click(index, id) {
            console.log(index, id);
            this.openAlert = true
        }, openAlertDialog() {
            this.openAlert = true
        }, closeAlertDialog() {
            this.openAlert = false
        }
    },
    updated() { }
});

function get_info(id, ids, obj) {
    var datas = typeof (obj) === 'number' ? {
        head: id,
        ids: ids,
        page: obj
    } : {
            head: id,
            ids: ids
        };
    axios({
        method: "POST",
        baseURL: './api/index.php',
        headers: {
            "Content-type": "application/json; charset=utf-8"
        },
        data: JSON.stringify(datas)
    }).then(function (res) {
        if (res.data.code === '200') {
            set_vue(res.data.data, id);
            if (id == 107) {
                msg_fun()
            };
            if (id === 101 && obj === 0) {
                localStorage.setItem('book', (JSON.stringify(res.data.data)))
            }
        } else {
            if (id === 101) {
                app.item_data = false
            };
            console.log(res.data.msg)
        }
    }).
        catch(function (err) {
            console.log(err)
        })
};

function set_vue(obj, id) {
    var d = obj;
    var len = d.length;
    if (id === 107 || id === 108) {
        if (id === 107) {
            app.msg_data = obj
        };
        if (id == 108) {
            for (var i = 0; i < len; i++) {
                if (d[i].ad_img) {
                    if (d[i].ad_img.includes('../../../')) {
                        d[i].ad_img = d[i].ad_img.split('../../.')[1]
                    }
                }
            };
            app.ad_data = d
        }
    } else {
        for (var i = 0; i < len; i++) {
            d[i].href = './view/item/' + (d[i].id || d[i].Id) + '.html';
            if (d[i].book_img) {
                if (d[i].book_img.includes('../../../')) {
                    d[i].book_img = d[i].book_img.split('../../.')[1]
                }
            } else {
                d[i].book_img = img_null
            }
        }
    };
    if (id === 100) {
        var d = localStorage.getItem("nav");
        if (!d) {
            app.nav = obj;
            localStorage.setItem("nav", (JSON.stringify(obj)))
        } else {
            if (d === JSON.stringify(obj)) {
                app.nav = obj
            } else {
                app.nav = obj;
                localStorage.setItem("nav", (JSON.stringify(obj)))
            }
        }
    };
    if (id === 101) {
        for (var i = 0; i < d.length; i++) {
            if (d[i]) {
                app.item.push(d[i]);
                if (i === d.length - 1) {
                    var arr_sort = [];
                    app.item = app.item.reduce(function (item, next) {
                        arr_sort[next.Id] ? '' : arr_sort[next.Id] = true && item.push(next);
                        return item
                    }, []);
                    set_img()
                }
            }
        };
        show_like()
    };
    if (id === 102) {
        var d = localStorage.getItem("tag");
        if (!d) {
            app.tag = obj;
            localStorage.setItem("tag", (JSON.stringify(obj)))
        } else {
            if (d === JSON.stringify(obj)) {
                app.tag = obj
            } else {
                app.tag = obj;
                localStorage.setItem("tag", (JSON.stringify(obj)))
            }
        }
    };
    if (id === 103) {
        var d = localStorage.getItem("new");
        if (!d) {
            app.new_items = obj;
            localStorage.setItem("new", (JSON.stringify(obj)))
        } else {
            if (d === JSON.stringify(obj)) {
                app.new_items = obj
            } else {
                app.new_items = obj;
                localStorage.setItem("new", (JSON.stringify(obj)))
            }
        }
    };
    if (id === 104) {
        var d = localStorage.getItem("hot");
        if (!d) {
            app.hot_items = obj;
            localStorage.setItem("hot", (JSON.stringify(obj)))
        } else {
            if (d === JSON.stringify(obj)) {
                app.hot_items = obj
            } else {
                app.hot_items = obj;
                localStorage.setItem("hot", (JSON.stringify(obj)))
            }
        }
    };
    if (id === 105) {
        var d = localStorage.getItem("link");
        if (!d) {
            app.links = obj;
            localStorage.setItem("link", (JSON.stringify(obj)))
        } else {
            if (d === JSON.stringify(obj)) {
                app.links = obj
            } else {
                app.links = obj;
                localStorage.setItem("link", (JSON.stringify(obj)))
            }
        }
    }
};
if (localStorage.getItem('nav')) {
    app.nav = JSON.parse(localStorage.getItem('nav'));
    setTimeout(function () {
        get_info(100)
    }, app.timer)
} else {
    get_info(100)
};
if (localStorage.getItem('book')) {
    set_vue((JSON.parse(localStorage.getItem('book'))), 101);
    setTimeout(function () {
        get_info(101, 0, 0)
    }, app.timer)
} else {
    setTimeout(function () {
        get_info(101, 0, 0)
    }, 200)
};
if (localStorage.getItem('tag')) {
    app.tag = JSON.parse(localStorage.getItem('tag'));
    setTimeout(function () {
        get_info(102)
    }, app.timer)
} else {
    setTimeout(function () {
        get_info(102)
    }, 500)
};
if (localStorage.getItem('new')) {
    app.new_items = JSON.parse(localStorage.getItem('new'));
    setTimeout(function () {
        get_info(103)
    }, app.timer)
} else {
    setTimeout(function () {
        get_info(103)
    }, 800)
};
if (localStorage.getItem('hot')) {
    app.hot_items = JSON.parse(localStorage.getItem('hot'));
    setTimeout(function () {
        get_info(104)
    }, app.timer)
} else {
    setTimeout(function () {
        get_info(104)
    }, 1000)
};
if (localStorage.getItem('link')) {
    app.links = JSON.parse(localStorage.getItem('link'));
    setTimeout(function () {
        get_info(105)
    }, app.timer)
} else {
    setTimeout(function () {
        get_info(105)
    }, 1500)
};
setTimeout(function () {
    get_info(107)
}, 1500);
setTimeout(function () {
    get_info(108)
}, 2000);
window.onresize = function () {
    var w = document.documentElement.clientWidth || document.body.clientWidth;
    if (w <= 830) {
        app.expands = false
    } else {
        app.expands = true
    }
};
var w = document.documentElement.clientWidth || document.body.clientWidth;
if (w <= 830) {
    app.expands = false
} else {
    app.expands = true
};

function msg_fun() {
    setTimeout(function () {
        if (app.msg_data) {
            var box = document.querySelector('.msg ul');
            var list = document.querySelectorAll('.msg ul li');
            var h = list[0] ? list[0].offsetHeight : 0;
            var num = 0;
            setInterval(function () {
                box.style.marginTop = num + 'px';
                var h1 = box.offsetHeight;
                if (h1 < num * -1 + h * 2) {
                    num = 0;
                    box.style.transition = 'all 0s'
                } else {
                    box.style.transition = 'all 0.5s';
                    num -= h
                }
            }, 1500)
        }
    }, 1000)
};
(function () {
    var bp = document.createElement('script');
    var curProtocol = window.location.protocol.split(':')[0];
    if (curProtocol === 'https') {
        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'
    } else {
        bp.src = 'http://push.zhanzhang.baidu.com/push.js'
    }
    var s = document.getElementsByTagName("script")[0];
    s.parentNode.insertBefore(bp, s)
})();

function show_like() {
    setTimeout(function () {
        var arr = localStorage.getItem('like') ? JSON.parse(localStorage.getItem('like')) : [];
        var len = arr.length || 0;
        var liles = document.querySelectorAll('.like');
        var liles_len = liles.length || 0;
        for (var i = 0; i < len; i++) {
            for (var j = 0; j < liles_len; j++) {
                if (arr[i] == app.item[j].Id) {
                    liles[j].classList.add('like_clicks')
                }
            }
        }
    }, 500)
};

function set_img(obj) {
    var obj = obj || 1000;
    setTimeout(function () {
        var img = document.querySelectorAll('.img');
        var page = app.page;
        page++;
        page *= 10;
        var num = app.num * page / 10;
        for (var i = (page - 10); i < num; i++) {
            if (img[i]) {
                img[i].src = img[i].getAttribute('data-img')
            }
        }
    }, obj)
};

function get_user_info() {
    var data = {
        href: window.location.href,
        title: document.title,
        timer: 5000,
        source: document.referrer
    };
    axios({
        method: "POST",
        baseURL: './api/log.php',
        headers: {
            "Content-type": "application/json; charset=utf-8"
        },
        data: JSON.stringify(data)
    }).then(function (res) { }).
        catch(function (err) {
            console.log(err)
        })
};
setTimeout(function () {
    get_user_info()
}, 5000);